const ImageCycleResizer = require('image-cycle-resizer')
const {opendir} = require('fs/promises')
const path = require('path')
// 质量
const QUALITY = 70;  // 取值 0-100

const input_dir = './DirInput/'
const output_dir = './DirOutput/'


async function main() {
    try {
        const dir = await opendir(input_dir)
        for await (const dirent of dir) {
            if (dirent.name !== '.gitignore') {
                compressImage(dirent.name, input_dir, output_dir)
            }
        }
    } catch (err) {
        console.error(err)
    }
}


function compressImage(imageName, inputDir, outDir) {
    const imageOriginPath = path.join(inputDir, imageName)
    const imageOutputPath = path.join(outDir, imageName)
    console.log('开始处理图片', imageOriginPath)
    let image = new ImageCycleResizer(imageOriginPath)
    image.setQuality(QUALITY).save(imageOutputPath)
    console.log('压缩存储到', imageOriginPath)
}

main().then(() => {
})

